//
//  MapThemeSwitchDemoViewController.m
//  FMDemoBaseMap
//
//  Created by fengmap on 16/10/17.
//  Copyright © 2016年 fengmap. All rights reserved.
//
//  主题切换
//  简介：在线主题切换，离线主题切换；

#import "MapThemeSwitchDemoViewController.h"
#import "FMMapKit.h"

// 通过修改 IsOnline 的值 1/0 来修改在线/离线状态
#define IsOnline 0

@interface MapThemeSwitchDemoViewController () <FMKMapViewDelegate>
{
    FMKMapView *_mapView;
    UIView *_themeBtnView;
    NSMutableArray *_btnArray;
}

@end

@implementation MapThemeSwitchDemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 离线地图加载
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"jlai-cyds-museum02" ofType:@"fmap"];
    _mapView = [[FMKMapView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) path:dataPath delegate:self];
    [self.view addSubview:_mapView];
    
    [self createButton];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createButton
{
    UIButton *themeSwitchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    themeSwitchBtn.frame = CGRectMake(0, SCREEN_HEIGHT-44, SCREEN_WIDTH, 44);
    [themeSwitchBtn setTitle:@"切换主题" forState:UIControlStateNormal];
    [themeSwitchBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    themeSwitchBtn.backgroundColor = [UIColor whiteColor];
    themeSwitchBtn.layer.shadowColor = [UIColor grayColor].CGColor;
    themeSwitchBtn.layer.shadowOpacity = 0.6;
    themeSwitchBtn.layer.shadowOffset = CGSizeMake(1, 1);
    [themeSwitchBtn addTarget:self action:@selector(themeSwitchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:themeSwitchBtn];
    
    _themeBtnView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-44-90, SCREEN_WIDTH, 90)];
    [self.view addSubview:_themeBtnView];
    _themeBtnView.hidden = YES;
    
    _btnArray = [NSMutableArray array];
    for (int i = 0; i < 2; i++) {
        UIButton *themeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        themeBtn.frame = CGRectMake(0, i*45, SCREEN_WIDTH, 44);
        [themeBtn setTitle:[NSString stringWithFormat:@"主题%d: %d", i+1, i+2001] forState:UIControlStateNormal];
        [themeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [themeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [themeBtn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] size:themeBtn.frame.size] forState:UIControlStateNormal];
        [themeBtn setBackgroundImage:[UIImage imageWithColor:FMDemoColor size:themeBtn.frame.size] forState:UIControlStateSelected];
        themeBtn.layer.shadowColor = [UIColor grayColor].CGColor;
        themeBtn.layer.shadowOpacity = 0.6;
        themeBtn.layer.shadowOffset = CGSizeMake(1, 1);
        themeBtn.tag = i+1;
        [themeBtn addTarget:self action:@selector(themeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_themeBtnView addSubview:themeBtn];
        [_btnArray addObject:themeBtn];
        
        if (i == 0) {
            themeBtn.selected = YES;
        }
    }
}

#pragma mark - FMKMapViewDelegate
- (void)mapViewDidFinishLoadingMap:(FMKMapView *)mapView {
    NSLog(@"地图加载完成");
}
- (void)mapViewDidFailLoadingMap:(FMKMapView *)mapView withError:(NSError *)error {
    NSLog(@"地图加载失败-%@", error);
}

#pragma mark - 切换主题按钮点击
- (void)themeSwitchBtnClick:(UIButton *)sender
{
    _themeBtnView.hidden = !_themeBtnView.hidden;
}

#pragma mark - 主题按钮点击
- (void)themeBtnClick:(UIButton *)sender
{
    sender.selected = YES;
    if (IsOnline) {
        // 在线主题切换
        if (sender.tag == 1) {
            ((UIButton *)_btnArray[1]).selected = NO;
            // 通过主题ID设置主题
            [_mapView setThemeWithID:@"2001"];
        }
        else if (sender.tag == 2) {
            ((UIButton *)_btnArray[0]).selected = NO;
            [_mapView setThemeWithID:@"2002"];
        }
    }
    else {
        // 离线主题切换
        if (sender.tag == 1) {
            ((UIButton *)_btnArray[1]).selected = NO;
            // 通过主题数据路径设置地图主题
            NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"2001/2001.theme"];
            [_mapView setThemeWithLocalPath:themePath];
        }
        else if (sender.tag == 2) {
            ((UIButton *)_btnArray[0]).selected = NO;
            NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"2002/2002.theme"];
            [_mapView setThemeWithLocalPath:themePath];
        }
    }

}

@end
